(function(){
    "use strict"

    function goHome() {
        window.open('http://elewen.oschina.io/')
    }

    let isStart = false
    let timer = 0
    let timerBox = document.querySelector('.timer')
    let score = 0
    let scoreBox = document.querySelector('.score')
    let bug = document.querySelector('.bug')
    let header = document.querySelector('.header')
    let body = document.querySelectorAll('li')
    let food = document.querySelector('.food')
    let highMaxBox = document.querySelector('.high_max')
    let highMax = localStorage.getItem('tanchishse_high_max')
    highMaxBox.innerText = highMax
    const micro = 60
    //    console.log(body)
    // (0-40)*10 px

    let coor_h = {
        x: 3,
        y: 0
    }
    let coor_f = {
        x: '',
        y: ''
    }

    // 做饭
    function newFood() {
        coor_f.x = Math.floor(Math.random() * 39)
        coor_f.y = Math.floor(Math.random() * 39)

        food.style.left = 10 * coor_f.x + 'px'
        food.style.top = 10 * coor_f.y + 'px'
        food.style.backgroundColor = 'rgb(' + Math.floor(Math.random() * 256) + ',' + Math.floor(Math.random() * 256) + ',' + Math.floor(Math.random() * 256) + ')'
        Math.floor(Math.random())
        // console.log(123, coor_f)
    }

    newFood()
    newFood()

    // 位置初始化
    for (let i = 0; i < body.length; i++) {
        // [3,0],[2,0][1,0]
        // console.log(i)
        body[i].style.left = 10 * (coor_h.x - i) + 'px'
        body[i].style.top = 10 * coor_h.y + 'px'
        body[i].style.backgroundColor = 'rgb(' + Math.floor(Math.random() * 256) + ',' + Math.floor(Math.random() * 256) + ',' + Math.floor(Math.random() * 256) + ')'
    }

    // 方向初始化
    let direction = 'r'

    // console.log(coor_h)

    function startGame() {
        // 计时器
        let clock = setInterval(function () {
            timer += 1
            timerBox.innerText = (Math.floor(timer / 3600) <= 9 ? '0' : '') +
                Math.floor(timer / 3600) + ':' + (Math.floor(timer / 60) <= 9 ? '0' : '') +
                Math.floor(timer / 60) + ':' + (timer % 60 <= 9 ? '0' : '') + timer % 60
        }, 1000)

        // 定时器
        let step = setInterval(function () {
            // 改变位置
            switch (direction) {
                case 'r':
                    coor_h.x += 1
                    break
                case 'l':
                    coor_h.x -= 1
                    break
                case 'b':
                    coor_h.y += 1
                    break
                case 't':
                    coor_h.y -= 1
                    break
            }

            // console.log(coor_h)

            // 是否越界,若越界，则游戏结束
            if (coor_h.x > 39 || coor_h.x < 0 || coor_h.y > 39 || coor_h.y < 0) {
                clearInterval(clock)
                clearInterval(step)
                if (localStorage.getItem('tanchishse_high_max') < score * 10) {
                    localStorage.setItem('tanchishse_high_max', score * 10)
                }
                console.log('game over!')
                var cons_r = confirm('game over! 重新开始吗？')
                if (cons_r == true) {
                    location.reload()
                }
                return
            }

            // 记录前一个的位置
            let coor_bef = {
                x: body[0].style.left,
                y: body[0].style.top
            }

//        console.log(coor_bef)

            // 刷新位置
            header.style.left = 10 * coor_h.x + 'px'
            header.style.top = 10 * coor_h.y + 'px'

            // 吃饭
            if (coor_h.x == coor_f.x && coor_h.y == coor_f.y) {
                let li = document.createElement('li')
                li.style.left = 10 * coor_f.x + 'px'
                li.style.top = 10 * coor_f.y + 'px'
                li.style.backgroundColor = food.style.backgroundColor
                bug.appendChild(li)
                body = document.querySelectorAll('li')
//            console.log('吃掉啦！')
//            console.log(coor_h, coor_f, coor_h == coor_f)
                setTimeout(function () {
                    newFood()
                }, 100)
                score += 1
                scoreBox.innerText = score * 10
            }

            for (let i = 1; i < body.length; i++) {
                // 记录后一个的位置
                let coor_aft = {
                    x: body[i].style.left,
                    y: body[i].style.top
                }
                body[i].style.left = coor_bef.x
                body[i].style.top = coor_bef.y
                coor_bef = coor_aft
//            console.log(i, body[i], body[i].style.left, body[i].style.top)
            }
        }, micro)
    }

    function oEvent(e) {
        if (isStart == false) {
            startGame()
            isStart = true
        }

        if ([37, 38, 39, 40, 65, 68, 83, 87].indexOf(e.keyCode) >= 0) {

            // 换方向
            switch (e.keyCode) {
                case 87:
                case 38:
                    // t
                    direction = direction === 'b' ? 'b' : 't'
                    break
                case 83:
                case 40:
                    // b
                    direction = direction === 't' ? 't' : 'b'
                    break
                case 65:
                case 37:
                    // l
                    direction = direction === 'r' ? 'r' : 'l'
                    break
                case 68:
                case 39:
                    // r
                    direction = direction === 'l' ? 'l' : 'r'
                    break
            }

            // 产生新2

        } else {
            // console.log('点击无效')
        }
    }

    //    键盘事件监听
    document.onkeydown = oEvent

    //    ------------------------触摸屏滑动操作----------------------------------------------
    let coor = {
        pre: {
            x: '',
            y: ''
        },
        nex: {
            x: '',
            y: ''
        },
        changed: {
            x: '',
            y: ''
        }
    }

    function go(ev) {
        let e = ev || event
        coor.pre.x = e.changedTouches[0].screenX
        coor.pre.y = e.changedTouches[0].screenY
    }

    function stop(ev) {
        if (isStart == false) {
            startGame()
            isStart = true
        }

        let e = ev || event
        let word = ''
//        console.log(e)

        coor.nex.x = e.changedTouches[0].screenX
        coor.nex.y = e.changedTouches[0].screenY
        coor.changed.x = coor.nex.x - coor.pre.x
        coor.changed.y = coor.nex.y - coor.pre.y

        if (coor.changed.x == 0 && coor.changed.y == 0) {
            word = '未移动'
            console.log(word)
            return
        }

        if (Math.abs(coor.changed.x) > Math.abs(coor.changed.y)) {
            word = '水平'
            if (coor.changed.x > 0) {
                word += '向右'
                direction = direction === 'l' ? 'l' : 'r'
            } else {
                word += '向左'
                direction = direction === 'r' ? 'r' : 'l'
            }
        } else {
            word = '竖直'
            if (coor.changed.y > 0) {
                word += '向下'
                direction = direction === 't' ? 't' : 'b'
            } else {
                word += '向上'
                direction = direction === 'b' ? 'b' : 't'
            }
        }
//        console.log(word)
    }
})()